package com.jxaummd.mushroom.main.data.fragment;


import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;

import com.github.mikephil.charting.charts.LineChart;
import com.github.mikephil.charting.components.AxisBase;
import com.github.mikephil.charting.components.Description;
import com.github.mikephil.charting.components.Legend;
import com.github.mikephil.charting.components.LimitLine;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.Entry;
import com.github.mikephil.charting.data.LineData;
import com.github.mikephil.charting.data.LineDataSet;
import com.github.mikephil.charting.formatter.IAxisValueFormatter;
import com.github.mikephil.charting.formatter.IValueFormatter;
import com.github.mikephil.charting.utils.ColorTemplate;
import com.github.mikephil.charting.utils.ViewPortHandler;
import com.jxaummd.mushroom.R;
import com.jxaummd.mushroom.R2;
import com.jxaummd.mushroom.core.base.fragment.BaseBottomFragment;
import com.jxaummd.mushroom.core.util.tool.time.MTimeUtils;
import com.jxaummd.mushroom.main.data.beam.DataBean;
import com.jxaummd.mushroom.main.data.contract.DataContract;
import com.jxaummd.mushroom.main.data.presenter.DataPresenter;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;

public class DataFragment extends BaseBottomFragment<DataContract.Presenter>
        implements DataContract.View {

    @BindView(R2.id.lc_data_tem_chart)
    LineChart mChartTemp = null;

    @BindView(R2.id.lc_data_hum_chart)
    LineChart mChartHum = null;

    public static DataFragment newInstance() {
        Bundle args = new Bundle();
        DataFragment fragment = new DataFragment();
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onItemSelect(int position) {

    }

    /**
     * 初始化Presenter
     *
     * @return Presenter
     */
    @Override
    protected DataContract.Presenter initPresenter() {
        return new DataPresenter(this);
    }

    /**
     * 设置layout
     *
     * @return
     */
    @Override
    public Object setLayout() {
        return R.layout.data_layout;
    }

    /**
     * 视图逻辑绑定
     *
     * @param savedInstanceState
     * @param rootView
     */
    @Override
    public void onBindView(@Nullable Bundle savedInstanceState, View rootView) {
        mPresenter.getdata(1);
    }


    public void setChartStyle(LineChart mChartTemp, LineDataSet dataSet, int min, int max) {
        LineChart lineChart;
        XAxis xAxis;                //X轴
        YAxis leftYAxis;            //左侧Y轴
        YAxis rightYaxis;           //右侧Y轴
        Legend legend;              //图例

        dataSet.setLineWidth(1f);
        dataSet.setDrawFilled(true);
        dataSet.setFormLineWidth(1f);
        dataSet.setFormSize(30.f);
        dataSet.setColor(Color.GRAY);
        dataSet.setMode(LineDataSet.Mode.CUBIC_BEZIER);
        //不显示点点
        dataSet.setDrawCircles(false);

        LineData lineData = new LineData(dataSet);

        mChartTemp.setData(lineData);
        mChartTemp.invalidate();
        mChartTemp.setBackgroundColor(Color.WHITE);
        //网格线
        mChartTemp.setDrawGridBackground(false);
        //是否显示边界
        mChartTemp.setDrawBorders(false);
        //是否可以拖动
//        mChartTemp.setDragEnabled(false);
        //是否有触摸事件
        mChartTemp.setTouchEnabled(true);

        /***XY轴的设置***/
        xAxis = mChartTemp.getXAxis();
        leftYAxis = mChartTemp.getAxisLeft();
        rightYaxis = mChartTemp.getAxisRight();

        xAxis.setDrawGridLines(false);
        rightYaxis.setDrawGridLines(false);
        leftYAxis.setDrawGridLines(true);
        leftYAxis.setAxisMaximum(max);
        leftYAxis.setAxisMinimum(min);
        leftYAxis.enableGridDashedLine(10f, 10f, 0f);
        leftYAxis.setValueFormatter((value, axis) -> value + "C");
        rightYaxis.setEnabled(false);

        //X轴设置显示位置在底部
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setAxisMinimum(0f);
        xAxis.setGranularity(1f);
        xAxis.setValueFormatter((value, axis) -> {
            String hour = "";
            int h = Integer.valueOf(MTimeUtils.getNowHour()) - (int) value;
            if (h < 0) {
                h = h + 24;
            }
            return h + ":00";
        });
        //保证Y轴从0开始，不然会上移一点
        leftYAxis.setAxisMinimum(0f);

        /***折线图例 标签 设置***/
        legend = mChartTemp.getLegend();
        //设置显示类型，LINE CIRCLE SQUARE EMPTY 等等 多种方式，查看LegendForm 即可
        legend.setForm(Legend.LegendForm.LINE);
        legend.setTextSize(12f);
        //显示位置 左下方
        legend.setVerticalAlignment(Legend.LegendVerticalAlignment.BOTTOM);
        legend.setHorizontalAlignment(Legend.LegendHorizontalAlignment.LEFT);
        legend.setOrientation(Legend.LegendOrientation.HORIZONTAL);
        //是否绘制在图表里面
        legend.setDrawInside(false);

        Drawable drawable = getResources().getDrawable(R.drawable.data_fade_blue);
        setChartFillDrawable(drawable, mChartTemp);

        //描述
        Description description = new Description();
        description.setEnabled(false);
        mChartTemp.setDescription(description);
    }

    /**
     * 设置线条填充背景颜色
     *
     * @param drawable
     */
    public void setChartFillDrawable(Drawable drawable, LineChart lineChart) {
        if (lineChart.getData() != null && lineChart.getData().getDataSetCount() > 0) {
            LineDataSet lineDataSet = (LineDataSet) lineChart.getData().getDataSetByIndex(0);
            //避免在 initLineDataSet()方法中 设置了 lineDataSet.setDrawFilled(false); 而无法实现效果
            lineDataSet.setDrawFilled(true);
            lineDataSet.setFillDrawable(drawable);
            lineChart.invalidate();
        }
    }


    @Override
    public void updateChart(List<DataBean> beans) {
//        mPresenter.getdata(1);
        //加入数据
        List<Entry> humentrys = new ArrayList<>();
        List<Entry> tementrys = new ArrayList<>();
        for (int i = 0; i < beans.size(); i++) {
            humentrys.add(new Entry(i, beans.get(i).getMb_temp(), Math.random() * 30));
            tementrys.add(new Entry(i, beans.get(i).getMb_hum(), Math.random() * 30));
        }
        LineDataSet humdateSet = new LineDataSet(tementrys, "湿度");
        humdateSet.setLabel("湿度");
        humdateSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) -> (value + "'C         ").substring(0, 6));
        LineDataSet temdataSet = new LineDataSet(humentrys, "温度");
        temdataSet.setLabel("湿度");
        temdataSet.setValueFormatter((value, entry, dataSetIndex, viewPortHandler) ->{
            String da = (value + "'C  ").substring(0, 6);
            return da;
        }
        );

        setChartStyle(mChartTemp, temdataSet, 10, 30);
        setChartStyle(mChartHum, humdateSet, 0, 100);
    }
}
